package org.darcy.framework.blog.plugin.file;

import java.util.Map;

import org.darcy.config.property.AppProperties;
import org.darcy.entity.BizFile;
import org.darcy.entity.SysUser;
import org.darcy.framework.blog.business.enums.ConfigKeyEnum;
import org.darcy.framework.blog.exception.BlogException;
import org.darcy.framework.blog.file.AliyunOssApiClient;
import org.darcy.framework.blog.file.ApiClient;
import org.darcy.framework.blog.file.LocalApiClient;
import org.darcy.framework.blog.file.QiniuApiClient;
import org.darcy.framework.blog.file.entity.VirtualFile;
import org.darcy.framework.blog.file.exception.GlobalFileException;
import org.darcy.framework.blog.holder.SpringContextHolder;
import org.darcy.framework.util.BeanConvertUtil;
import org.darcy.framework.util.SessionUtil;
import org.darcy.service.BizFileService;
import org.darcy.service.SysConfigService;
import org.springframework.util.StringUtils;

/**
 * 文件上传工具
 */
public class BaseFileUploader {

	ApiClient getApiClient(String uploadType) {
		SysConfigService configService = SpringContextHolder.getBean(SysConfigService.class);
		Map<String, Object> config = configService.getConfigs();
		String storageType = null;
		if (null == config
				|| StringUtils.isEmpty((storageType = (String) config.get(ConfigKeyEnum.STORAGE_TYPE.getKey())))) {
			throw new BlogException("[文件服务]当前系统暂未配置文件服务相关的内容！");
		}
		AppProperties p = SpringContextHolder.getBean(AppProperties.class);
		ApiClient res = null;
		switch (storageType) {
		case "local":
			res = new LocalApiClient().init(p.getLocalPath(), uploadType);
			break;
		case "qiniu":
			String accessKey = (String) config.get(ConfigKeyEnum.QINIU_ACCESS_KEY.getKey()),
					secretKey = (String) config.get(ConfigKeyEnum.QINIU_SECRET_KEY.getKey()),
					qiniuBucketName = (String) config.get(ConfigKeyEnum.QINIU_BUCKET_NAME.getKey()),
					baseUrl = (String) config.get(ConfigKeyEnum.QINIU_BASE_PATH.getKey());
			res = new QiniuApiClient().init(accessKey, secretKey, qiniuBucketName, baseUrl, uploadType);
			break;
		case "aliyun":
			String endpoint = (String) config.get(ConfigKeyEnum.ALIYUN_ENDPOINT.getKey()),
					accessKeyId = (String) config.get(ConfigKeyEnum.ALIYUN_ACCESS_KEY.getKey()),
					accessKeySecret = (String) config.get(ConfigKeyEnum.ALIYUN_ACCESS_KEY_SECRET.getKey()),
					url = (String) config.get(ConfigKeyEnum.ALIYUN_FILE_URL.getKey()),
					aliYunBucketName = (String) config.get(ConfigKeyEnum.ALIYUN_BUCKET_NAME.getKey());
			res = new AliyunOssApiClient().init(endpoint, accessKeyId, accessKeySecret, url, aliYunBucketName,
					uploadType);
			break;
		case "youpaiyun":
			break;
		default:
			break;
		}
		if (null == res) {
			throw new GlobalFileException("[文件服务]当前系统暂未配置文件服务相关的内容！");
		}
		return res;
	}

	VirtualFile saveFile(VirtualFile virtualFile, boolean save, String uploadType) {
		if (save) {
			BizFileService fileService = SpringContextHolder.getBean(BizFileService.class);
			try {
				SysConfigService configService = SpringContextHolder.getBean(SysConfigService.class);
				Map<String, Object> config = configService.getConfigs();
				String storageType = (String) config.get(ConfigKeyEnum.STORAGE_TYPE.getKey());

				BizFile fileInfo = BeanConvertUtil.doConvert(virtualFile, BizFile.class);
				SysUser sessionUser = SessionUtil.getUser();
				fileInfo.setUserId(null == sessionUser ? null : sessionUser.getId());
				fileInfo.setUploadType(uploadType);
				fileInfo.setStorageType(storageType);
				fileService.insert(fileInfo);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return virtualFile;
	}

	boolean removeFile(ApiClient apiClient, String filePath, String uploadType) {
		BizFileService fileService = SpringContextHolder.getBean(BizFileService.class);
		BizFile file = fileService.selectFileByPathAndUploadType(filePath, uploadType);
		String fileKey = filePath;
		if (null != file) {
			fileKey = file.getFilePath();
		}
		return apiClient.removeFile(fileKey);
	}

}
